package com.gionee.navil.lockscreen;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class GnNavilIndicator extends FrameLayout {

	private final static String TAG = "NavilIndicator";
	private final static long DURATION = 300;

	private int mIndicatorW;
	private int mCountPage = 5;
	private int mCurPage = 2;
	private ImageView mBgView;
	private ImageView mIndicatorView;
	private static int sDisplayW = 0;

	public GnNavilIndicator(Context context) {
		this(context, null);
	}

	public GnNavilIndicator(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public GnNavilIndicator(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		sDisplayW = context.getResources().getDisplayMetrics().widthPixels;
		mBgView = new ImageView(context);
		// mBgView.setBackgroundColor(Color.RED);
		mBgView.setImageDrawable(new Drawable() {

			@Override
			public void setColorFilter(ColorFilter cf) {
				// TODO Auto-generated method stub

			}

			@Override
			public void setAlpha(int alpha) {
				// TODO Auto-generated method stub

			}

			@Override
			public int getOpacity() {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public void draw(Canvas canvas) {
				canvas.drawColor(Color.LTGRAY, PorterDuff.Mode.SRC);

			}
		});
		FrameLayout.LayoutParams flp = new LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		addView(mBgView, flp);
		mIndicatorView = new ImageView(context);
		mIndicatorView.setImageDrawable(new Drawable() {

			@Override
			public void setColorFilter(ColorFilter cf) {
				// TODO Auto-generated method stub

			}

			@Override
			public void setAlpha(int alpha) {
				// TODO Auto-generated method stub

			}

			@Override
			public int getOpacity() {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public void draw(Canvas canvas) {
				canvas.drawColor(Color.DKGRAY, PorterDuff.Mode.SRC);

			}
		});
		// mIndicatorView.setBackgroundColor(Color.BLUE);
		addView(mIndicatorView, flp);
		Log.d(TAG, "NavilIndicator");
		Log.d(TAG, "mBgView.getMeasuredWidth()=" + mBgView.getMeasuredWidth()
				+ ",mBgView.getMeasuredHeight()=" + mBgView.getMeasuredHeight());
		Log.d(TAG, "mBgView.getWidth()=" + mBgView.getWidth()
				+ ",mBgView.getHeight()=" + mBgView.getHeight());

		Log.d(TAG,
				"mIndicatorView.getMeasuredWidth()="
						+ mIndicatorView.getMeasuredWidth()
						+ ",mIndicatorView.getMeasuredHeight()="
						+ mIndicatorView.getMeasuredHeight());
		Log.d(TAG, "mIndicatorView. getWidth()=" + mIndicatorView.getWidth()
				+ ",mIndicatorView.getHeight()=" + mIndicatorView.getHeight());
		// mIndicatorView.setVisibility(View.INVISIBLE);

	}

	public void setCountPage(int countPage) {
		mCountPage = countPage;
	}

	public void setCurPage(int curPage) {
		mCurPage = curPage;
		requestLayout();
	}

	public int getCurPage() {
		return mCurPage;
	}

	public int getCountPage() {
		return mCountPage;
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
//		Log.d(TAG, "onLayout");
//		Log.d(TAG, "mBgView.getMeasuredWidth()=" + mBgView.getMeasuredWidth()
//				+ ",mBgView.getMeasuredHeight()=" + mBgView.getMeasuredHeight());
//		Log.d(TAG, "mBgView.getWidth()=" + mBgView.getWidth()
//				+ ",mBgView.getHeight()=" + mBgView.getHeight());
//
//		Log.d(TAG,
//				"mIndicatorView.getMeasuredWidth()="
//						+ mIndicatorView.getMeasuredWidth()
//						+ ",mIndicatorView.getMeasuredHeight()="
//						+ mIndicatorView.getMeasuredHeight());
//		Log.d(TAG, "mIndicatorView. getWidth()=" + mIndicatorView.getWidth()
//				+ ",mIndicatorView.getHeight()=" + mIndicatorView.getHeight());
		if (mCountPage > 0) {
			mIndicatorW = mBgView.getWidth() / mCountPage;

			int l = getPaddingLeft() + mIndicatorW * mCurPage;
			Log.d(TAG, "l=" + l + ",mIndicatorW=" + mIndicatorW);
			mIndicatorView.setLeft(l);
			mIndicatorView.setRight(l + mIndicatorW);
			mCurPageScreenCenter = sDisplayW / 2 + mCurPage * sDisplayW;
			Log.d(TAG,"mCurPageScreenCenter="+mCurPageScreenCenter);
		}

//		Log.d(TAG,
//				"mIndicatorView.getMeasuredWidth()="
//						+ mIndicatorView.getMeasuredWidth()
//						+ ",mIndicatorView.getMeasuredHeight()="
//						+ mIndicatorView.getMeasuredHeight());
//		Log.d(TAG, "mIndicatorView. getWidth()=" + mIndicatorView.getWidth()
//				+ ",mIndicatorView.getHeight()=" + mIndicatorView.getHeight());

	}
//
//	@Override
//	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//		Log.d(TAG, "onMeasure");
//		Log.d(TAG, "mBgView.getMeasuredWidth()=" + mBgView.getMeasuredWidth()
//				+ ",mBgView.getMeasuredHeight()=" + mBgView.getMeasuredHeight());
//		Log.d(TAG, "mBgView.getWidth()=" + mBgView.getWidth()
//				+ ",mBgView.getHeight()=" + mBgView.getHeight());
//
//		Log.d(TAG,
//				"mIndicatorView.getMeasuredWidth()="
//						+ mIndicatorView.getMeasuredWidth()
//						+ ",mIndicatorView.getMeasuredHeight()="
//						+ mIndicatorView.getMeasuredHeight());
//		Log.d(TAG, "mIndicatorView. getWidth()=" + mIndicatorView.getWidth()
//				+ ",mIndicatorView.getHeight()=" + mIndicatorView.getHeight());
//
//	}
//
//	@Override
//	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//		super.onSizeChanged(w, h, oldw, oldh);
//		Log.d(TAG, "onSizeChanged w=" + w + ",h=" + h);
//		Log.d(TAG, "mBgView.getMeasuredWidth()=" + mBgView.getMeasuredWidth()
//				+ ",mBgView.getMeasuredHeight()=" + mBgView.getMeasuredHeight());
//		Log.d(TAG, "mBgView.getWidth()=" + mBgView.getWidth()
//				+ ",mBgView.getHeight()=" + mBgView.getHeight());
//
//		Log.d(TAG,
//				"mIndicatorView.getMeasuredWidth()="
//						+ mIndicatorView.getMeasuredWidth()
//						+ ",mIndicatorView.getMeasuredHeight()="
//						+ mIndicatorView.getMeasuredHeight());
//		Log.d(TAG, "mIndicatorView. getWidth()=" + mIndicatorView.getWidth()
//				+ ",mIndicatorView.getHeight()=" + mIndicatorView.getHeight());
//	}
	
	private TimeInterpolator mTimeInterpolator = new TimeInterpolator() {
		
		@Override
		public float getInterpolation(float input) {
			return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
		}
	};

	public void nextPage() {
		if (mCurPage < (mCountPage - 1)) {
			mCurPage++;
			requestLayout();
			mIndicatorView.setScaleType(ScaleType.FIT_XY);
			mIndicatorView.setPivotX(mIndicatorW);
			AnimatorSet as = new AnimatorSet();
			as.setInterpolator(mTimeInterpolator);
			as.setDuration(DURATION);
			as.play(ObjectAnimator.ofFloat(mIndicatorView, "scaleX", 1f));
			as.start();

		}
	}

	public void prePage() {
		if (mCurPage > 0) {
			mCurPage--;
			requestLayout();
			mIndicatorView.setScaleType(ScaleType.FIT_XY);
			mIndicatorView.setPivotX(0);
			AnimatorSet as = new AnimatorSet();
			as.setInterpolator(mTimeInterpolator);
			as.setDuration(DURATION);
			as.play(ObjectAnimator.ofFloat(mIndicatorView, "scaleX", 1f));
			as.start();
		}
	}
	private int mCurPageScreenCenter = 0;

	
	public void move(int screenCenter) {
		int dx = screenCenter - mCurPageScreenCenter; 
		if (dx > 0) {
			float scaleX = 1 + ((float)dx/sDisplayW);
			Log.d(TAG,"dx="+dx+",scaleX="+scaleX);
			rightScalX(scaleX);
		} else if (dx < 0) {
			float scaleX = 1 - ((float)dx/sDisplayW);
			Log.d(TAG,"dx="+dx+",scaleX="+scaleX);
			leftScalX(scaleX);
		}
		
	}
	
	public void leftScalX(float scaleX) {
		mIndicatorView.setScaleType(ScaleType.FIT_XY);
		mIndicatorView.setPivotX(mIndicatorW);
		mIndicatorView.setScaleX(scaleX);
	}
	
	public void rightScalX(float scaleX) {
		mIndicatorView.setScaleType(ScaleType.FIT_XY);
		mIndicatorView.setPivotX(0);
		mIndicatorView.setScaleX(scaleX);
	}

}
